package com.lzymall.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: lizhenyao
 * @Desciption:
 */
@Slf4j
@Component
public class ExceptionResolver implements HandlerExceptionResolver{
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        log.error("{} Exception", httpServletRequest.getRequestURI(), e);

        //当使用是jackson2.x的时候使用MappingJackson2JsonView,课程使用的是1.9
        ModelAndView mv = new ModelAndView(new MappingJacksonJsonView());
        mv.addObject("status", ResponseCode.ERROR.getCode());
        mv.addObject("msg", "接口异常,详情请看服务端的后台日志");
        mv.addObject("data", e.toString());
        return mv;
    }
}
